# Contributor: Chris Kruger <alpine@krugerheavyindustries.com>
# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=vaultwarden
pkgver=1.32.3
pkgrel=0
pkgdesc="Bitwarden alternative backend"
_bw_web_ver=2024.6.2c
provides="bitwarden_rs=$pkgver-r$pkgrel"
replaces="bitwarden_rs"
url="https://github.com/dani-garcia/vaultwarden"
arch="all !ppc64le !s390x !riscv64 !loongarch64" # ring crate has platform specific asm - no ppc support, no rust on s390x
license="AGPL-3.0-only"
makedepends="
	cargo
	cargo-auditable
	libpq-dev
	mariadb-dev
	mimalloc2-dev
	openssl-dev
	sqlite-dev
	"
subpackages="$pkgname-web-vault::noarch $pkgname-openrc"
install="$pkgname.pre-install $pkgname.post-upgrade"
pkgusers="vaultwarden"
pkggroups="vaultwarden"
source="$pkgname-$pkgver.tar.gz::https://github.com/dani-garcia/vaultwarden/archive/$pkgver.tar.gz
	$pkgname-webvault-$_bw_web_ver.tar.gz::https://github.com/dani-garcia/bw_web_builds/releases/download/v$_bw_web_ver/bw_web_v$_bw_web_ver.tar.gz
	$pkgname.initd
	$pkgname.confd"

# secfixes:
#   1.32.0:
#     - CVE-2024-39924
#     - CVE-2024-39925
#     - CVE-2024-39926

prepare() {
	default_prepare

	mkdir -p .cargo

	local target="$(rustc -vV | sed -n 's/host: //p')"

	# Build against system-provided mimalloc.
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		mimalloc = { rustc-link-lib = ["mimalloc"] }
	EOF

	cargo fetch --target="$CTARGET" --locked
}

build() {
	export VW_VERSION=$pkgver-r$pkgrel
	cargo auditable build --release --features sqlite,postgresql,mysql,enable_mimalloc --frozen
}

check() {
	cargo test --release --features sqlite,postgresql,mysql,enable_mimalloc --frozen
}

package() {
	install -Dm755 target/release/vaultwarden -t "$pkgdir"/usr/bin/
	install -Dm640 .env.template -o vaultwarden -g vaultwarden -t "$pkgdir"/var/lib/vaultwarden

	(cd "$srcdir"/web-vault; find . -type f -exec \
		install -Dm644 {} "$pkgdir"/usr/share/webapps/vaultwarden-web/{} \;)
	(cd "$builddir"/src/static; find . -type f -exec \
		install -Dm644 {} "$pkgdir"/usr/share/webapps/vaultwarden-web/{} \;)

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm655 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

vault() {
	pkgdesc="$pkgdesc (web ui)"

	amove usr/share/webapps

	# move templates back
	mkdir -p "$pkgdir"/usr/share/webapps/vaultwarden-web
	mv "$subpkgdir"/usr/share/webapps/vaultwarden-web/templates "$pkgdir"/usr/share/webapps/vaultwarden-web/
}

sha512sums="
2753968257258b16acbd8a0153497f4835210470878f6a086056db5f98bb23f8c346042fd7d72d7726ff5e7d0039675c0d6a902bdac475c0c9339e31380ec8af  vaultwarden-1.32.3.tar.gz
2bb7f565c7c57f6fd8b1c96df71dc4a6ddec7d16e63c1ecb2a0c0a7adefe062eb55274cea2c2c1ff4e9ba42d12050a3eb2d6a093e4d501a37d7855e369bf0914  vaultwarden-webvault-2024.6.2c.tar.gz
2b11e83be40665877f201979fa850546dceb69beb07f41a5ef44639bc1b191a0735e6ad865bc6e5e77481ea2e2426f4533cc863cb536fb9e2d422ad8761d293a  vaultwarden.initd
ad35ad5314600abb184cf28c78e0937efac4426f43d5bf60f72a1c41256ce31ae07e26c2fb5bf0108c49bd9d16596618cb0f5337c7dad33972310bd84bf68957  vaultwarden.confd
"
